/*
 * @Author: your name
 * @Date: 2021-11-12 21:33:25
 * @LastEditTime: 2021-11-29 13:45:54
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\serve\app\service\article.js
 */
/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-11-11 09:09:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
class TagService extends Service {
    //获取标签
    async getTagList() {
            const { ctx, app } = this;
            const res = await app.mysql.select("tag");
            return res;
        }
        //获取标签个数
    async getTagTotal() {
            const { ctx, app } = this;
            const res = await app.mysql.select("tag");
            const total = res.length;
            return total;
        }
        //添加标签
    async addTagList() {
            const { ctx, app } = this;
            const { text, value } = ctx.request.body;
            //判断是否存在
            const isHaveText = await app.mysql.get("tag", { text });
            const isHaveValue = await app.mysql.get("tag", { value });
            //提示拒绝添加
            if (isHaveText) {
                return {
                    isHave: true,
                    message: "重复的标签名",
                };
            } else if (isHaveValue) {
                return {
                    isHave: true,
                    message: "重复的标签值",
                };
            } else {
                const res = await app.mysql.insert("tag", { text, value });
                return res;
            }
        }
        //删除标签
    async delTagList() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        const res = await app.mysql.delete("tag", {
            id,
        });
        return res;
    }
    async editTagList() {
            const { ctx, app } = this;
            const { id, value, text } = ctx.request.body;
            const isHaveText = await app.mysql.get("tag", { text }); //是否存在同类text值
            const isHaveValue = await app.mysql.get("tag", { value }); //是否存在同类value值
            //判断是否有同value
            if (isHaveValue) {
                //判断id是否与修改id相同
                if (isHaveValue.id !== id) {
                    return {
                        isHave: true,
                        message: "标签值已被占用",
                    };
                } else {
                    //判断是否有同text值
                    if (isHaveText) {
                        //判断id是否与修改id相同
                        if (isHaveText.id !== id) {
                            return {
                                isHave: true,
                                message: "标签名已被占用",
                            };
                        }
                    }
                }
            }
            //判断是否有同text值
            if (isHaveText) {
                //判断id是否与修改id相同
                if (isHaveText.id !== id) {
                    return {
                        isHave: true,
                        message: "标签名已被占用",
                    };
                } else {
                    //判断是否有同value
                    if (isHaveValue) {
                        //判断id是否与修改id相同
                        if (isHaveValue.id !== id) {
                            return {
                                isHave: true,
                                message: "标签值已被占用",
                            };
                        }
                    }
                }
            }
            //否则更新
            const res = await app.mysql.update("tag", {
                id,
                value,
                text,
            });
            return res;
        }
        //************用户端******************* */
        //获取标签
    async getClientTag() {
            const { ctx, app } = this;
            let tag = await app.mysql.select("tag");
            const article = await app.mysql.select("allarticle");

            tag.forEach((item, index) => {
                article.forEach((val) => {
                    if (val.state * 1) {
                        if (item.text === val.tag) {
                            if (tag[index].num) {
                                tag[index].num = tag[index].num + 1;
                            } else {
                                tag[index].num = 1;
                            }
                        }
                    }
                });
            });
            return tag;
        }
        //获取标签文章
    async getTagArticle() {
        const { ctx, app } = this;
        const { text } = ctx.params;
        let res = [],
            result = [];
        const article = await app.mysql.select("allarticle");
        article.forEach((item) => {
            if (item.state * 1) {
                res.push(item);
            }
        });
        res.sort((a, b) => {
            return (
                new Date(b.release_time).getTime() - new Date(a.release_time).getTime()
            );
        });

        res.forEach((item) => {
            if (item.tag === text) {
                result.push(item);
            }
        });
        return result;
    }
}
module.exports = TagService;